//
//  MainViewController.m
//  微博
//
//  Created by 沈艳男 on 15/10/24.
//  Copyright © 2015年 沈艳男. All rights reserved.
//

#import "MainViewController.h"
#import "HomeTableViewController.h"
#import "DiscoverTableViewController.h"
#import "MessageTableViewController.h"
#import "ProfileTableViewController.h"

@interface MainViewController ()
@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addChildViewController];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self composedButton];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (UIButton *)composedButton
{
    UIButton *btn = [UIButton new];
    NSInteger count = self.childViewControllers.count;
    CGFloat w = self.tabBar.bounds.size.width / count;
    CGRect rect = CGRectMake(0, 0, w, self.tabBar.bounds.size.height);
    btn.frame = CGRectOffset(rect, 2 * w, 0);
    
    [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
    [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];

    [self.tabBar addSubview:btn];
    return btn;
}

- (void)btnClick
{
    NSLog(@"btnClick");
}

- (void)addChildViewController
{
    self.tabBar.tintColor = [UIColor orangeColor];
    HomeTableViewController *home = [HomeTableViewController new];
    [self addChildViewController:home title:@"首页" imageName:@"tabbar_home"];
    DiscoverTableViewController *discover = [DiscoverTableViewController new];
    [self addChildViewController:discover title:@"发现" imageName:@"tabbar_discover"];
    
    UIViewController *viewController = [UIViewController new];
    [self addChildViewController:viewController];
    
    
    MessageTableViewController *message = [MessageTableViewController new];
    [self addChildViewController:message title:@"信息" imageName:@"tabbar_message_center"];
    ProfileTableViewController *profile = [ProfileTableViewController new];
    [self addChildViewController:profile title:@"我" imageName:@"tabbar_profile"];
}

- (void)addChildViewController:(UIViewController *)vc title:(NSString *)title imageName:(NSString *)imageName
{
    vc.title = title;
    vc.tabBarItem.image = [UIImage imageNamed:imageName];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    [self addChildViewController:nav];
}




@end
